Imports System.IO

Partial Class FolderSelect
    Inherits System.Web.UI.UserControl
    Private mStartVerz As String
    Private mAuswahl As String
    Private mAuswahl1 As String
    Private mRoot As String
    Public Property StartVerz() As String
        Get
            StartVerz = mStartVerz
        End Get
        Set(ByVal value As String)
            mStartVerz = value
        End Set
    End Property

    Public Property Root() As String
        Get
            Root = mRoot
        End Get
        Set(ByVal value As String)
            mRoot = value
        End Set
    End Property
 
    Public Property Auswahl() As String
        Get
            Auswahl = mAuswahl
        End Get
        Set(ByVal value As String)
            mAuswahl = value
        End Set
    End Property

    Public Property Auswahl1() As String
        Get
            Auswahl1 = mAuswahl1
        End Get
        Set(ByVal value As String)
            mAuswahl1 = value
        End Set
    End Property

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim strPath As String = MapPath(mStartVerz)
        Dim strXml As String = "<" & Root & ">"
        strXml = strXml + ProcessFolder(strPath)
        strXml = strXml + "</" & Root & ">"
        Me.XmlDataSource1.Data = strXml
    End Sub

    Private Function ProcessFolder(ByVal strStartFolder As String) As String
        Dim strXml As String = ""
        Dim strFolders() As String
        Dim strFolder As String
        Dim strFolderRelative As String

        strFolders = Directory.GetDirectories(strStartFolder, "*")
        For Each strFolder In strFolders
            If Path.GetFileName(strFolder) <> "_vti_cnf" Then
                'strFolderRelative = strFolder.Replace("\", "/")
                'strFolderRelative = strFolderRelative.Substring(strFolderRelative.LastIndexOf(StartVerz.Replace("~", "")))
                'strXml = strXml + "<folder id='" + strFolderRelative + "'>"

                strXml = strXml + "<folder id='" + Path.GetFileName(strFolder) + "'>"
                strXml = strXml + ProcessFolder(strFolder)
                strXml = strXml + "</folder>"
            End If
        Next
        'Dim strFiles() As String
        'Dim strFile As String
        'strFiles = Directory.GetFiles(strStartFolder, "*")
        'For Each strFile In strFiles
        '    strXml = strXml + "<file id='" + Path.GetFileName(strFile) + "'></file>"
        'Next
        Return strXml
    End Function

    Protected Sub TreeView1_SelectedNodeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TreeView1.SelectedNodeChanged
        TuEs(e)
    End Sub

    Protected Sub butOK_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles butOK.Click
    End Sub

    Private Sub TuEs(ByVal e As System.EventArgs)
        Auswahl = TreeView1.SelectedNode.Text
        If Auswahl <> Root Then
            Dim n As TreeNode = TreeView1.SelectedNode.Parent
            Do Until n.Text = Root
                Auswahl = n.Text & "/" & Auswahl
                n = n.Parent
            Loop
            auswahl1 = Auswahl
            Auswahl = StartVerz & "/" & Auswahl
        End If
        RaiseEvent OKClick(Me, e)
    End Sub
    Protected Sub butCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles butCancel.Click
        RaiseEvent OKClick(Me, e)
        Auswahl = ""
    End Sub

    Public Event OKClick(ByVal sender As Object, ByVal e As System.EventArgs)
End Class
